<?php

/**
 * Getter/Setter Generator
 * Helps you generate standard getter and setter for your classes
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.Tools
 * @since 1.2
 */

echo '<!DOCTYPE html>
<html><head><title>phpRaise Getter/Setter Generator</title>
        <style type="text/css"><!--
            body{font:normal 12px/1.6 Tahoma}a:visited,a{font-family:Lucida Sans;text-decoration:none;color:#EB0}a:hover{text-decoration:underline;color:#FC3}.container{width:630px;margin:20px auto 100px}.header{font:bold 25px Lucida Sans;margin-bottom:10px;color:#666;height:90px}.header,.header .sub{margin-left:10px}.header .sub{font:normal 11px/2.2 Verdana;margin-top:5px;color:#AAA;border-left:5px solid #EEE;background:#f6f6f6;margin-right:30px}.header .sub,.entries .header{padding-left:10px}.entries,.entries .sysinfo .info:hover{background:#EEE}.entries,.entries .sysinfo,.entries .sysinfo .info,.entries .header,.entries .summary,#ftr{padding:5px}.entries,.entries .sysinfo .info{clear:both}.entries .resultset{text-shadow:1px 1px 3px #555}.entries .sysinfo{width:600px;margin:5px auto;background:#FFF}.entries .sysinfo .info{text-align:right;border-bottom:1px solid #EEE}.entries .sysinfo .info .label{float:left;color:#888}.entries .header{font:16px Tahoma;color:#222;height:auto}.entries .name{font:18px Tahoma}.entries .entry_warning,.entries .entry_message,.entries .entry_error,.entries .final{padding:5px 10px}.entries .entry_warning,.entries .entry_message,.entries .entry_error{margin:5px 20px;border-left:5px solid #CCC}.entries .entry_error{background:#A00}.entries .entry_error,.entries .entry_message,.entries .entry_warning,.entries .summary{color:#FFF}.entries .entry_message{background:#999}.entries .entry_warning{background:#EA1}.entries .summary{margin:5px;background:#666}.entries .final{color:#333;text-shadow:1px 1px 0 #FFF}#ftr{border:5px solid #EEE}#abtLink,#abtCloseLink{display:none}#abtCloseLink{float:right;}b.tooltip{font-weight:normal;cursor:help; border-bottom:1px dashed #CCC;}
            --></style>
    </head>
    <body><div class="container"><div class="header">Getter/Setter Generator<div class="sub">Standardized Getter/Setter Generation Tool for <a href="http://code.google.com/p/phpraise" target="_blank">phpRaise</a> by <a href="http://twitter.com/thephpdeveloper" target="_blank">Sam-Mauris Yong</a><br />Source code licensed under <a href="http://www.opensource.org/licenses/bsd-license" target="_blank">New BSD License</a></div></div><div class="entries">';
echo '<p>Paste your entire class, with all the private variables and PHP documentation blocks, in the text box below and click Go.</p>';
echo '<div class="header">Entire Class</div><div class="entry_message">';
echo '<form action="gsgenerate.php" method="post"><textarea name="code" style="width: 99%;height:200px;">' . ($_POST ? htmlentities($_POST['code']) : '') . '</textarea><br /><input type="submit" value="Go" /></form>';
echo '</div>';

function varType($phpDoc){
    preg_match('`\@var ([a-zA-Z0-1_-|]+)\s`is', $phpDoc, $matches);
    return count($matches) >= 2 ? $matches[1] : '';
}

if (count($_POST) > 0){

    echo '<div class="header">Getter Setter Output</div><div class="entry_message">';
    $tokens = token_get_all('<?php ' . $_POST['code']);

    $vs = array();
    $tps = array();
    $insideClass = false;
    $insideMethod = false;
    foreach ($tokens as $tk) {
        if (is_array($tk)) {
            if($tk[0] == T_CLASS){
                $insideClass = true;
            }
            if($tk[0] == T_FUNCTION){
                $insideMethod = true;
            }
            if($insideClass && !$insideMethod){
                if ($tk[0] == T_DOC_COMMENT){
                    $tps[] = varType($tk[1]);
                }
                if ($tk[0] == T_VARIABLE) {
                    $vs[] = $tk[1];
                }
            }
        }
    }

    $buffer = '';

    foreach ($vs as $k => $v) {
        $v = substr($v, 1);
        $type = array_key_exists($k, $tps) ? $tps[$k] : '$type$';
        $buffer .= '
    /**
     * Get or set $description$
     * @param ' . $type . ' $' . $v[0] . ' (optional) $description$
     * @return ' . $type . '
     */
    public function ' . $v . '($' . $v[0] . ' = null){
        if(func_num_args() == 1){
            $this->' . $v . ' = $' . $v[0] . ';
        }
        return $this->' . $v . ';
    }
';
    }

    echo '<pre><code>' . $buffer . '</code></pre>';
    echo '</div>';
}

echo '</div></div></body></html>';